package com.its.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.its.member.mapper.MemberMapper;
import com.its.member.pojo.Member;
import com.its.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lenovo
 * @description 针对表【gym_member】的数据库操作Service实现
 * @createDate 2025-04-22 21:05:11
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {
    //依赖注入Mapper层属性
    @Autowired
    private MemberMapper memberMapper;


    //新增会员
    @Override
    public Member addMember(Member member) {
        if (member.getMemberAccount() == null) {
            QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByDesc("member_account");
            queryWrapper.last("limit 1");
            Member lastMember = memberMapper.selectOne(queryWrapper);
            if (lastMember == null) {
                member.setMemberAccount(1001);
            } else {
                member.setMemberAccount(lastMember.getMemberAccount() + 1);
            }
        }
        if (member.getMemberPassword() == null) {
            member.setMemberPassword("123456");
        }
        memberMapper.insert(member);
        return member;
    }

    //删除会员
    @Override
    public int delMember(Integer memberAccount) {
        return memberMapper.deleteById(memberAccount);
    }

    //修改会员信息
    @Override
    public int updateMember(Member member) {
        return memberMapper.updateById(member);
    }

    //根据ID查询会员信息
    @Override
    public Member selectMemberById(Long mid) {
        return memberMapper.selectById(mid);
    }

    //根据会员账户查询会员信息
    @Override
    public Member selMemberByMemberCountService(Integer memberAccount) {
        if(memberAccount != null){
            QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("member_account",memberAccount);
            Member member = memberMapper.selectOne(queryWrapper);
            return member;
        }else {
            return null;
        }

    }

    //分页查询会员信息
    @Override
    public Page<Member> selectMemberByPage(Integer page, Integer size) {
        return memberMapper.selectPage(new Page<>(page, size), null);
    }
}




